<?php 
include('../../Connections/KRAPPERCON.php');
$found = false;
$oper = getVal('oper');
	$cldoc = '';
	$newref = '';
	$facref = '';
	$ccli = '';
	$fnow = '';
	$ncli = '';
	$sub = 0;
	$igv = 0;
	$tot = 0;

// Cargar Factura de referencia	
if ($oper == 'load'){
$combo = '';
include('../../Query_Min/getAlmacenes.php');

$factura = getVal('factura');

		$sql = "call sp_toFactNC('".$factura."','".$user_now."')";
		$res = $mysqli->query($sql) or die($mysqli->error.__LINE__);
		if ($res) {
			if ($row = $res->fetch_assoc()){
			if ($row['factura']>0){
			$found = true;
			}
			if ($row['status'] != 0 ){
				//echo "<script type='text/javascript'>showError('No se pueden referenciar Doc.Anulados o de Anulacion');
				?>                	
				<h2>El Documento&nbsp;<a style="font-weight:bold" href="#" id="sp_new_nc" onClick="loadPageFact($(this).html())"><?php echo $factura;?></a>&nbsp; no se puede referenciar si es Doc.Anulado o de Anulacion</h2>
			<?php
				exit;
			}
			elseif ($row['cladoc'] != 'FC_BL' && $row['cladoc'] != 'FC_FC'){
			?>                	
				<h2>El Documento&nbsp;<a style="font-weight:bold" href="#" id="sp_new_nc" onClick="loadPageFact($(this).html())"><?php echo $factura;?></a>&nbsp; no es Factura ni Boleta de Venta.</h2>
			<?php
				exit;
			}
			
			$cldoc = $row['newcla'];
			$newref = $row['newfol'];
			$facref = $row['folio'];
			$ccli = $row['codcli'];
			$fnow = $row['hoy'];
			$ncli = $row['nomcli'];
				/*
				GEBI('cbo_nc_cldoc').value = '<?php echo $cldoc; ?>';
		GEBI('t_nc_folio').value = '<?php echo $newref; ?>';
		$('#sp_nc_nodoc').html('<?php echo $factura;?>');
		$('#sp_nc_nodoc').html('<?php echo $facref; ?>');
		GEBI('t_nc_subt').value = '<?php echo $subt; ?>';
		GEBI('t_nc_igv').value = '<?php echo $igv; ?>';
		GEBI('t_nc_total').value = '<?php echo $tot; ?>';
		$('#sp_nc_ccli').html('<?php echo $ccli; ?>');
		$('#sp_nc_ncli').html('<?php echo $ncli; ?>');
				*/
			}
		$res->close();
		$mysqli->check_result();
		}
	if(!$found) {
	?>
	<h2>El Documento&nbsp;<?php echo $factura;?>&nbsp;no existe</h2>
	<?php
	}
	else {
	$edit = 0;
	include ('ResItemNC.php');
	?>
	<script type="text/javascript">
	$('#tbl_nc_dat input,#tbl_nc_dat select,#tbl_nc_dat textarea').enable();
	GEBI('cbo_nc_cldoc').value = 'FC_N1';
	GEBI('t_nc_folio').value = '<?php echo $newref; ?>';
	$('#sp_nc_nodoc').html('<?php echo $factura;?>');
	$('#sp_nc_noref').html('<?php echo $facref; ?>');
	GEBI('t_nc_subt').value = myRound('<?php echo $subt; ?>');
	GEBI('t_nc_fdoc').value = '<?php  echo $fnow; ?>';
	GEBI('t_nc_igv').value = myRound('<?php echo $subt_igv; ?>');
	GEBI('t_nc_total').value = myRound('<?php echo $subt+$subt_igv; ?>');
//	$('#sp_nc_ccli').html('<?php echo $ccli; ?>');
//	$('#sp_nc_ncli').html('<?php echo $ncli; ?>');
	$('#cbo_nc_cli').val('<?php echo $ccli; ?>');
	GEBI('t_nc_text').value = '';
	//alert('');
	calcNetNC(null);
	</script>
	<?php 
	}
} 
elseif( $oper == 'save' ) 
{
	$fact = getVal('factura');
	$fecha = getVal('fecha');
	$clase = getVal('clase');
	$items = getVal('items');
	$txt = getVal('texto');
	$client = getVal('cliente');
	
	$sql = "call sp_saveNOTCRE('".$fact."','".$fecha."','".$clase."','".$items."','".$txt."','".$client."','".$user_now."');";
	doEcho($sql);
	$res = $mysqli->query($sql) or die($mysqli->error.__LINE__);	
	if ($res){
		if ($row = $res->fetch_assoc()){
			if ($row['newnro'] > 0){
				echo "<script type='text/javascript'>
				showOk('Se ha registrado correctamente el Documento N/C ".$row['newnro']." con referencia ".$row['newfol']."');
				$('#sp_new_nc').html('".$row['newnro']."');
				$('#t_nc_folio').html('".$row['newfol']."');
				$('#tbl_nc_dat input,#tbl_nc_dat select,#tbl_nc_dat textarea').disable();
				</script>";
			}
			else {
				echo "<script type='text/javascript'>showError('".$row['msg_err']."');</script>";
			}
		}
	$res->close();
	$mysqli->check_result();
	}
}

elseif ($oper == 'reload'){
$combo = '';
include('../../Query_Min/getAlmacenes.php');
$factura = getVal('factura');
$clase = getVal('clase');
	$edit = 0;
	// Si es N/C por valor, se deben ingresar valores y la Ctd no es modificable
	if ($clase == 'FC_N3'){
	$edit = 1;
	}
		include('ResItemNC.php');	

}

?>




